/*
 * OREKIT-X
 * Copyright 2002-2008 CS Communication & Systemes
 * 
 * Parts of this software package have been licensed to CS
 * Communication & Systemes (CS) under one or more contributor license
 * agreements.  See the NOTICE file distributed with this work for
 * additional information.
 *  
 * This is an experimental copy of OREKIT from www.orekit.org.
 * Please use the original OREKIT from orekit.org for normal work
 * unrelated to this research project.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License.  You
 * may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package ore.time.date;

/** 
 * Class providing a years sequence compliant with the gregorian
 * calendar.
 * 
 * @author Luc Maisonobe
 */
public final class GregorianFactory
    extends Object 
    implements YearFactory
{
    public final static YearFactory Instance = new GregorianFactory();


    public GregorianFactory(){
        super();
    }


    public int getYear(final int j2000Day) {
        /*
         * Year estimate
         */
        int year = (400 * j2000Day + 292194288) / 146097;
        /*
         * The previous estimate is one unit too high in some rare cases
         * (240 days in the 400 years gregorian cycle, about 0.16%)
         */
        if (j2000Day <= getLastJ2000DayOfYear(year - 1)) {
            --year;
        }
        /*
         * Exact year
         */
        return year;
    }
    public int getLastJ2000DayOfYear(int year) {
        return (1461 * year) / 4 - year / 100 + year / 400 - 730120;
    }
    public boolean isLeap(int year) {
        return ((year % 4) == 0) && (((year % 400) == 0) || ((year % 100) != 0));
    }
}
